package com.itheima.controller;

import com.itheima.pojo.PmsBrand;
import com.itheima.service.PmsBrandService;
import com.itheima.vo.PageResultVo;
import com.itheima.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brand")
public class PmsBrandController {

    /**
     * 添加品牌
     */
    @Autowired
    private PmsBrandService pmsBrandService;

    @PostMapping("create")
    public ResponseEntity create(@RequestBody PmsBrand pmsBrand) {
        pmsBrandService.create(pmsBrand);
        return ResponseEntity.ok(Result.success(null));
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    ///brand/delete/batch
    @PostMapping("/delete/batch")
    public ResponseEntity deleteByIds(Integer[] ids) {
        pmsBrandService.deleteByIds(ids);
        return ResponseEntity.ok(Result.success(null));
    }

    /**
     * 根据id删除品牌
     *
     * @param id
     * @return
     */
    ///brand/delete/{id}
    @GetMapping("/delete/{id}")
    public ResponseEntity deleteById(@PathVariable("id") Integer id) {
        Integer[] ids = new Integer[]{1};
        ids[0] = id;
        pmsBrandService.deleteByIds(ids);
        return ResponseEntity.ok(Result.success(null));
    }

    /**
     * 模糊分页查询品牌
     *
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @return
     */
    ///brand/list
    @GetMapping("/list")
    public ResponseEntity findPages(@RequestParam(defaultValue = "1") Integer pageNum,
                                    @RequestParam(defaultValue = "10") Integer pageSize,
                                    String keyword) {
        PageResultVo<PmsBrand> pmsBrands = pmsBrandService.findPages(pageNum, pageSize, keyword);
        return ResponseEntity.ok(Result.success(pmsBrands));
    }

    /**
     * 查询全部品牌列表
     *
     * @return
     */
    ///brand/listAll
    @GetMapping("/listAll")
    public ResponseEntity listAll() {
        List<PmsBrand> pmsBrands = pmsBrandService.listAll();
        return ResponseEntity.ok(Result.success(pmsBrands));
    }


    /**
     * 批量修改厂家状态
     * @return
     */
    @PostMapping("/update/factoryStatus")
    public ResponseEntity updateFactoryStatus(Integer factoryStatus,Integer [] ids) {
        pmsBrandService.updateFactoryStatus(factoryStatus,ids);
        return ResponseEntity.ok(Result.success(null));
    }

    /**
     * 修改显示状态
     * @param showStatus
     * @param ids
     * @return
     */
    ///brand/update/showStatus
    @PostMapping("/update/showStatus")
    public ResponseEntity updateShowStatus(Integer showStatus,Integer [] ids) {
        pmsBrandService.updateShowStatus(showStatus,ids);
        return ResponseEntity.ok(Result.success(null));
    }

    /**
     * 根据id修改广告
     * @param
     * @param
     * @return
     */
    ///brand/update/{id}
    @PostMapping("/update/{id}")
    public ResponseEntity updateShowStatus(@PathVariable("id") Integer id,@RequestBody PmsBrand pmsBrand) {
        pmsBrand.setId(id.longValue());
        pmsBrandService.updateById(pmsBrand);
        return ResponseEntity.ok(Result.success(null));
    }

    ///brand/{id}

    @GetMapping("{id}")
    public ResponseEntity findById(@PathVariable("id") Integer id){
        PmsBrand pmsBrand=pmsBrandService.findById(id);
        return ResponseEntity.ok(Result.success(pmsBrand));
    }
}
